home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / sources / general.cp < prev    next >
Text File  |  1995-09-29  |  2KB  |  85 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. #if defined( macintosh) || defined( __MWERKS__)
  6.  
  7. #else
  8.     #include <strstream.h>
  9. #endif
  10.  
  11. #include <new.h>
  12.  
  13. #ifdef macintosh
  14.     #ifdef __SC__
  15.         #include <Pascal.h>
  16.     #endif
  17. #endif
  18.  
  19. #include "general.h"
  20.  
  21. int general::new_handler_installed = 0;
  22.  
  23. void CheckForError( const int errorno, const char *text)
  24. {    
  25.     if( errorno != 0)
  26.     {
  27.         #if defined( macintosh) || defined( __MWERKS__)
  28.             char buffer[ 256];
  29.             sprintf( buffer, "CheckForError: %ld ", errorno);
  30.             strcat( buffer, text);
  31.             CtoPstr( buffer);
  32.             DebugStr( (unsigned char *)buffer);
  33.         #else
  34.             //
  35.             // We want to call 'cout.operator<<' only one time since it puts up an alert if
  36.             // we are running in an application and we only want one such an alert.
  37.             // ('Warning: writing to standard output is not allowed' or something like that) 
  38.             // (940728: taking this out of the Mac builds, but keeping this proviso in)
  39.             //
  40.             ostrstream tempstream;
  41.             tempstream << "unrecoverable error: " << text << ", error# " << errorno << "\n";
  42.             
  43.             tempstream << ends;
  44.             
  45.             char *destring = tempstream.str();
  46.             
  47.             cerr << destring;
  48.             
  49.             delete destring;
  50.         #endif
  51.         exit( EXIT_FAILURE);
  52.     }
  53. }
  54.  
  55. general::general()
  56. {
  57.     if( !new_handler_installed)
  58.     {
  59.         set_new_handler( &report_out_of_memory_and_exit);
  60.         new_handler_installed = true;
  61.     }
  62. }
  63.  
  64. void report_error_and_exit( const char *message)
  65. {
  66.     #if defined( macintosh) || defined( __MWERKS__)
  67.         //
  68.         // Note: c2pstr _does_ modify message, but we are going to exit
  69.         // _real_soon_now_, so this will not be a big problem…
  70.         // 941208: Actually, it may be a problem (e.g. when message is inside
  71.         // a resource, in ROM, or the like)
  72.         //
  73.         CtoPstr( (char *)message);
  74.         DebugStr( (unsigned char *)message);
  75.     #else
  76.         cerr << message << flush;
  77.     #endif
  78.     exit( EXIT_FAILURE);
  79. }
  80.  
  81. void report_out_of_memory_and_exit()
  82. {
  83.     report_error_and_exit( "operator new failed: out of memory. Exiting...\n");
  84. }
  85.